﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;
using System.Security.Cryptography;
using System.Web.UI.WebControls;
using System.IO;


namespace mamnontuoitho7.AppCode
{
    public class General
    {
        public ConnectGetData _connectData;
        public General()
        {
            _connectData = new ConnectGetData();
         }


        /// <summary>
        /// Convert date in database (MM/dd/yyyy hh:mm:ss) to dd/mm/yyyy
        /// </summary>
        /// <param name="strDate">string date</param>
        /// <returns>String dd/mm/yyyy</returns>
        public String ConvertDate_1(String strDate)
        {
            return DateTime.Parse(strDate).ToString("dd/MM/yyyy").ToString();
        }

        /// <summary>
        /// Convert date in database (MM/dd/yyyy hh:mm:ss) to (dd/mm/yyyy hh:mm:ss)
        /// </summary>
        /// <param name="strDate">string date</param>
        /// <returns>String dd/mm/yyyy</returns>
        public String ConvertDate_1B(String strDate)
        {
            return DateTime.Parse(strDate).ToString("dd/MM/yyyy HH:mm:ss").ToString();
        }

        /// <summary>
        /// Convert date in database (MM/dd/yyyy hh:mm:ss) to (dd/mm/yyyy hh:mm  AM/PM)
        /// </summary>
        /// <param name="strDate">string date</param>
        /// <returns>String dd/mm/yyyy</returns>
        public String ConvertDate_1C(String strDate)
        {
            return DateTime.Parse(strDate).ToString("dd/MM/yyyy HH:mm tt").ToString();
        }


        /// <summary>
        /// Convert date in database (dd/MM/yyyy) or (dd/MM/yyyy hh:mm:ss GMT) to MM/dd/yyyy
        /// </summary>
        /// <param name="strDate">string date</param>
        /// <returns>String MM/dd/yyyy</returns>
        public String ConvertDate_2(String strDate)
        {
            
            if (strDate != null && strDate != "")
            {
                string[] splDate = strDate.Split(new char[] { '/', ' ' });
                strDate = splDate[1] + "/" + splDate[0] + "/" + splDate[2];
            }
            return strDate;
        }


        /// <summary>
        /// Convert Vietnamese string to Engish string
        /// </summary>
        /// <param name="str"> Vietnamese string</param>
        /// <returns>Englisth string</returns>
        public static string filter(string str)
        {

            string[] VietNamChar = new string[] 
            { 
                "aAeEoOuUiIdDyY", 
                "áàạảãâấầậẩẫăắằặẳẵ", 
                "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", 
                "éèẹẻẽêếềệểễ", 
                "ÉÈẸẺẼÊẾỀỆỂỄ", 
                "óòọỏõôốồộổỗơớờợởỡ", 
                "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", 
                "úùụủũưứừựửữ", 
                "ÚÙỤỦŨƯỨỪỰỬỮ", 
                "íìịỉĩ", 
                "ÍÌỊỈĨ", 
                "đ", 
                "Đ", 
                "ýỳỵỷỹ", 
                "ÝỲỴỶỸ"
            };

            //Thay thế và lọc dấu từng char      
            for (int i = 1; i < VietNamChar.Length; i++)
            {
                for (int j = 0; j < VietNamChar[i].Length; j++)
                    str = str.Replace(VietNamChar[i][j], VietNamChar[0][i - 1]);
            }
            return str;
        }


        /// <summary>
        /// cut string in introduce
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public String CutString(String str, int length)
        {
            Boolean flag = true;
            while (flag)
            {
                try
                {
                    string temp = str.Substring(str.IndexOf('<'), str.IndexOf('>') - str.IndexOf('<') + 1);
                    str = str.Replace(temp, "");
                    if (temp == "")
                        flag = false;
                }
                catch (Exception)
                {
                    flag = false;
                }
            }
            str = str.Length > length ? str.Substring(0, length).Substring(0, str.Substring(0, length).LastIndexOf(' ')) + " ..." : str;
            return str.Replace("  ", " ");
        }

        /// <summary>
        /// get the number of a table
        /// </summary>
        /// <param name="tableName"> table or view</param>
        /// <returns> int total of record</returns>
        public global::System.Nullable<int> TotalNumberOfTable(String tableName)
        {

            SqlConnection con = _connectData.GetConnect();

            con.Open();

            string sql = "SELECT COUNT(*)FROM " + tableName;
            SqlCommand cmd = new SqlCommand(sql, con);

            object returnValue;

            returnValue = cmd.ExecuteScalar();

            if (((returnValue == null) || (returnValue.GetType() == typeof(global::System.DBNull))))
            {
                return new global::System.Nullable<int>();
            }
            else
            {
                return new global::System.Nullable<int>(((int)(returnValue)));
            }
        }

       

        /// <summary>
        /// phép chia làm tròn lên
        /// </summary>
        /// <param name="numberpage"></param>
        /// <param name="pagestep"></param>
        /// <returns></returns>
        public int Divide(int numberpage, int pagestep)
        {
            return Convert.ToInt16(Math.Ceiling(Convert.ToDouble(numberpage) / Convert.ToDouble(pagestep)));
        }
        /// <summary>
        /// phép chia làm tron xuong
        /// </summary>
        /// <param name="numberpage"></param>
        /// <param name="pagestep"></param>
        /// <returns></returns>
        public int DivideFloor(int numberpage, int pagestep)
        {
            return Convert.ToInt16(Math.Floor(Convert.ToDouble(numberpage) / Convert.ToDouble(pagestep)));
        }

        /// <summary>
        /// Replace Html Special Character (&,',<,>)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string ReplaceSpecialCharacters(string input)
        {
            input = input.Replace("&", "&amp;");
            input = input.Replace("'", "&apos;");
            input = input.Replace("<", "&lt;");
            input = input.Replace(">", "&gt;");
            return input;
        }

        /// <summary>
        /// delete all files and sub folder in directory
        /// </summary>
        /// <param name="directory"></param>
        public void DeleteSubfolderAndItem(System.IO.DirectoryInfo directory)
        {
            foreach (System.IO.FileInfo file in directory.GetFiles())
                file.Delete();

            foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories())
            {

                DeleteSubfolderAndItem(subDirectory);
                System.Threading.Thread.Sleep(1000);
                if (subDirectory.Exists)
                    subDirectory.Delete(true);

                //   System.IO.Directory.Delete(subDirectory.ToString());
            }
        }



        //public void DeleteSubfolderAndItem(System.IO.DirectoryInfo directory)
        //{
        //    foreach (System.IO.FileInfo file in directory.GetFiles())
        //        file.Delete();

        //    foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories())
        //    {

        //        subDirectory.Delete(true);

        //        //   System.IO.Directory.Delete(subDirectory.ToString());
        //    }
        //}


        /// <summary>
        /// Encrypt a string
        /// </summary>
        /// <param name="input"></param>
        /// <param name="algorithm"></param>
        /// <returns></returns>
        public string ComputeHash(string input, HashAlgorithm algorithm)
        {
            Byte[] inputBytes = Encoding.UTF8.GetBytes(input);

            Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);

            return BitConverter.ToString(hashedBytes);
        }
    }
}